<?php
$dp = $detallePedido;
$ip = $itemsPedido;
App::uses('CakeNumber', 'Utility');
?>
<script type='text/javascript' src='<?php echo $this->webroot; ?>js/plugins/validationEngine/languages/jquery.validationEngine-es.js'></script>
<script type='text/javascript' src='<?php echo $this->webroot; ?>js/plugins/validationEngine/jquery.validationEngine.js'></script>    
<div id="loader"><?php echo $this->Html->image('loader.gif'); ?></div>
<div class="wrapper">

    <div class="sidebar">

        <div class="top">
            <a href="index.html" class="logo"></a>
            <div class="search">
                <div class="input-prepend">
                    <span class="add-on orange"><span class="icon-user icon-white"></span></span>
                    <input type="text" value="Administrador"/>
                </div>            
            </div>
        </div>

        <div class="nContainer">
            <?php echo $this->element('menu/principal'); ?>
            <a class="close">
                <span class="ico-remove"></span>
            </a>                
        </div>
        <div class="widget">
            <div class="datepicker"></div>
        </div>

    </div>

    <div class="body">

        <?php echo $this->element('sidebar/toolbar'); ?>

        <div class="content">

            <div class="page-header">
                <div class="icon">
                    <span class="ico-dollar"></span>
                </div>
                <h1>Pedido #<?php echo $dp['Pedido']['id']; ?> <small>Fecha: <?php echo $dp['Pedido']['created']; ?> Hora: <?php echo $dp['Pedido']['hora']; ?></small></h1>
            </div>

            <div class="row-fluid">
                <div class="span12">                

                    <div class="block">
                        <div class="data invoice">

                            <div class="row-fluid">
                                <div class="span3">
                                    <h4>Parametros</h4>
                                    <address>
                                        <?php $handly = $dp['Pedido']['handly'] * 100; ?>
                                        <?php $preHandly = $dp['Pedido']['handly']; ?>
                                        <?php $preDesaduanizacion = $dp['Pedido']['desaduanizacion']; ?>
                                        <?php $desaduanizacion = $preDesaduanizacion * 100; ?>
                                        <strong>Handly: </strong><?php echo $handly; ?>%<br>                                        
                                        <strong>Recepcion: </strong><?php echo $dp['Pedido']['recepcion']; ?><br>     
                                        <strong>Consolidacion: </strong><?php echo $dp['Pedido']['consolidacion']; ?><br> 
                                        <strong>Flete: </strong><?php echo $dp['Pedido']['flete']; ?><br> 
                                        <strong>Desaduanizacion: </strong><?php echo $desaduanizacion; ?>%<br>                                         
                                    </address>
                                </div>
                                <div class="span3">
                                    <h4>Cliente</h4>
                                    <address>
                                        <strong><?php echo $dp['Pedido']['email']; ?></strong><br>
                                        321 Streeet, Suite 300<br>
                                        San Francisco, CA 94107<br>
                                        <abbr title="Phone">P:</abbr> +38 (123) 456-7890
                                    </address>                                
                                </div>
                                <div class="span3"></div>
                                <div class="span3">
                                    <h4>Cotizacion</h4>
                                    <p><strong>Fecha pedido:</strong> Dec 20, 2012</p>
                                    <p><strong>Fecha envio:</strong> Dec 29, 2012</p>
                                    <div class="highlight">
                                        <strong>Amount Due:</strong> $2,351.50 <em>USD</em>
                                    </div>
                                </div>
                            </div>

                            <h3>Descripcion</h3>
                            <p>Detalle de los items</p>

                            <table class="table" width="100%">
                                <thead>
                                    <tr>
                                        <th width="50%">Url</th>
                                        <th width="10%">Peso (Libras)</th>
                                        <th width="10%">Peso (Kilos)</th>
                                        <th width="10%">Costo</th>
                                        <th width="10%">Tax</th>
                                        <th width="10%">Shipping</th>
                                        <th>Acciones</th>
                                    </tr>
                                </thead>
                                <?php
                                $pesoTotal = 0;
                                $costoTotal = 0;
                                $taxTotal = 0;
                                $shippingTotal = 0;
                                ?>
                                <tbody>
                                    <?php foreach ($ip as $i): ?>
                                        <?php $pesoAkilos = $i['Item']['peso'] * 0.45; ?>
                                        <tr>
                                            <td><?php echo $i['Item']['url']; ?></td>
                                            <td><?php echo $i['Item']['peso']; ?></td>
                                            <td><?php echo $pesoAkilos; ?> Kg</td>
                                            <td><?php echo $i['Item']['costo']; ?> Sus</td>
                                            <td><?php echo $i['Item']['tax']; ?> Sus</td>
                                            <td><?php echo $i['Item']['shipping']; ?> Sus</td>                                            
                                            <td>
                                                <a href="#Modal_<?php echo $i['Item']['id']; ?>" role="button" class="button green" data-toggle="modal">
                                                    <div class="icon"><span class="ico-pencil"></span></div>
                                                    <!--<a href="#fModal" role="button" class="btn" data-toggle="modal">Form dialog</a>-->
                                                </a>
                                            </td>
                                        </tr>            
                                        <?php
                                        $pesoTotal += $pesoAkilos;
                                        $costoTotal += $i['Item']['costo'];
                                        $taxTotal += $i['Item']['tax'];
                                        $shippingTotal += $i['Item']['shipping'];
                                        ?>
                                    <?php endforeach; ?>
                                </tbody>
                                <tfoot class="bar-danger">
                                    <tr>
                                        <th width="60%">Totales</th>
                                        <th width="10%"></th>
                                        <th width="10%"><?php echo $pesoTotal; ?> Kg</th>
                                        <th width="10%"><?php echo $costoTotal; ?> Sus</th>
                                        <th width="10%"><?php echo $taxTotal; ?> Sus</th>
                                        <th width="10%"><?php echo $shippingTotal; ?> Sus</th>
                                        <th></th>
                                    </tr>
                                </tfoot>
                            </table>
                            <?php $precioTotal = $costoTotal + $taxTotal + $shippingTotal; ?>

                            <div class="row-fluid">
                                <div class="span9"></div>
                                <div class="span3">
                                    <div class="total">                                        
                                        <div class="highlight">
                                            <strong><span>Costo Total:</span> $<?php echo $precioTotal; ?> <em>Sus</em></strong>
                                        </div>
                                    </div>
                                </div>
                            </div>

                        </div>

                    </div>    

                    <div class="block">
                        <div class="head green">
                            <div class="icon"><span class="ico-list-alt"></span></div>
                            <h2>Detalle de la cotizacion</h2>    
                            <ul class="buttons">
                                <li><a href="#" onClick="source('table_hover_check');
                                        return false;"><div class="icon"><span class="ico-info"></span></div></a></li>
                            </ul>                                                          
                        </div>                
                        <div class="data-fluid">
                            <table cellpadding="0" cellspacing="0" width="100%" class="table table-hover">
                                <thead>
                                    <tr>                                            
                                        <th width="25%">Parametros</th>
                                        <th width="25%">Valor</th>
                                        <th width="25%">Valor</th>
                                        <th width="25%">Total</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <tr>                                            
                                        <td><b>HANDLY</b></td>
                                        <td><?php echo $handly; ?>%</td>
                                        <td><?php echo $precioTotal; ?> Sus</td>
                                        <td>
                                            <?php $handlyCalculado = $preHandly * $precioTotal; ?>
                                            <?php if ($handlyCalculado <= 30): ?>
                                                <span class="label label-important"><?php echo $valorHandly = 30; ?></span> (<?php echo $handlyCalculado; ?>)
                                            <?php else: ?>   
                                                <?php $valorHandly = $handlyCalculado; ?>
                                                <span class="label label-success"><?php echo $handlyCalculado; ?></span>
                                            <?php endif; ?>
                                        </td>
                                    </tr>
                                    <tr>

                                        <td>
                                            <b>RECEPCION</b>
                                        </td>
                                        <td>
                                            <?php echo $dp['Pedido']['recepcion']; ?>
                                        </td>                                
                                        <td width="25%">
                                            <?php echo $pesoTotal; ?> Kg
                                        </td>      
                                        <td width="25%">
                                            <?php $recepcionCalculado = $dp['Pedido']['recepcion'] * $pesoTotal; ?>
                                            <?php if ($recepcionCalculado <= 30): ?>
                                                <span class="label label-important"><?php echo $valorRecepcion = 30; ?></span> (<?php echo $recepcionCalculado; ?>)
                                            <?php else: ?> 
                                                <?php $valorRecepcion = $recepcionCalculado; ?>
                                                <span class="label label-success"><?php echo $recepcionCalculado; ?></span>
                                            <?php endif; ?>
                                        </td>                                
                                    </tr>
                                    <tr>

                                        <td>
                                            <b>CONSOLIDACION</b>
                                        </td>
                                        <td>
                                            <?php echo $dp['Pedido']['consolidacion']; ?>
                                        </td>         
                                        <td width="25%">
                                            <?php echo $pesoTotal; ?> Kg
                                        </td>  
                                        <td width="25%">
                                            <?php $consolidacionCalculado = $dp['Pedido']['consolidacion'] * $pesoTotal; ?>
                                            <?php if ($consolidacionCalculado <= 30): ?>
                                                <span class="label label-important"><?php echo $valorConsolidacion = 30; ?></span> (<?php echo $consolidacionCalculado; ?>)
                                            <?php else: ?>                                                
                                                <?php $valorConsolidacion = $consolidacionCalculado; ?>
                                                <span class="label label-success"><?php echo $consolidacionCalculado; ?></span>
                                            <?php endif; ?>
                                        </td>                                  
                                    </tr>
                                    <tr>

                                        <td>
                                            <b>FLETE</b>
                                        </td>
                                        <td>
                                            <?php echo $dp['Pedido']['flete']; ?>
                                        </td>                                                                
                                        <td width="25%">
                                            <?php echo $pesoTotal; ?> Kg
                                        </td>   
                                        <td width="25%">
                                            <?php $fleteCalculado = $dp['Pedido']['flete'] * $pesoTotal; ?>
                                            <?php if ($fleteCalculado <= 30): ?>
                                                <span class="label label-important"><?php echo $valorFlete = 30; ?></span> (<?php echo $fleteCalculado; ?>)
                                            <?php else: ?>                                                
                                                <?php $valorFlete = $fleteCalculado; ?>
                                                <span class="label label-success"><?php echo $fleteCalculado; ?></span>
                                            <?php endif; ?>
                                        </td>                                  
                                    </tr>
                                    <tr>
                                        <td>
                                            <b>DESADUANIZACION</b>
                                        </td>       
                                        <td>
                                            <?php echo $desaduanizacion; ?>%
                                        </td>        
                                        <td width="25%">

                                        </td>    
                                        <td width="25%">
                                            <?php
                                            $desaduanizacionCalculado = ($precioTotal + (0.25 * $valorFlete)) * $preDesaduanizacion;
                                            //echo $precioTotal.'-'.$valorFlete.'-'.$preDesaduanizacion;
                                            ?>                                           
                                            <span class="label label-success"><?php echo CakeNumber::currency($desaduanizacionCalculado, 'USD'); ?></span>
                                        </td>                                
                                    </tr>                           
                                </tbody>
                                <?php
                                $valorFinal = $valorHandly + $valorRecepcion + $valorConsolidacion + $valorFlete + $desaduanizacionCalculado;
                                ?>
                                <tfoot>
                                    <tr>                                            
                                        <th width="25%"></th>
                                        <th width="25%"></th>
                                        <th width="25%">Total</th>
                                        <th width="25%"><?php echo CakeNumber::currency($valorFinal, 'USD'); ?></th>
                                    </tr>
                                </tfoot>
                            </table>       
                            <div class="row-fluid">                                
                                <div class="span4">
                                    <div class="block">
                                        <div class="head green">                        
                                            <div class="icon"><i class="ico-edit"></i></div>
                                            <a href="#modalParametros" role="button" class="button green" data-toggle="modal"><h2>Modificar Parametros</h2></a>
                                        </div>                                        
                                    </div>                          
                                </div>                                                 
                            </div>

                            <div class="row-fluid">
                                <div class="span4">
                                    <div class="block">
                                        <div class="head dblue">                                
                                            <h2>Block with head</h2>                                
                                        </div>
                                        <div class="data">
                                            Sample of simple block with head
                                        </div>
                                    </div>                          
                                </div>
                                <div class="span4">
                                    <div class="block">
                                        <div class="head blue">                        
                                            <div class="icon"><i class="ico-grid"></i></div>
                                            <h2>Block with head</h2>
                                        </div>
                                        <div class="data">
                                            Sample of block with head and icon
                                        </div>
                                    </div>                          
                                </div>
                                <div class="span4">
                                    <div class="block">
                                        <div class="head orange">
                                            <div class="icon"><i class="ico-grid"></i></div>
                                            <h2>Block with head</h2>                 
                                            <ul class="buttons">                                    
                                                <li>
                                                    <a href="#"><div class="icon"><span class="ico-cog"></span></div></a>
                                                    <ul class="dropdown-menu">
                                                        <li><a href="#">Add new</a></li>
                                                        <li><a href="#">Upload</a></li>
                                                        <li><a href="#">Search</a></li>
                                                        <li class="divider"></li>
                                                        <li><a href="#">Clear folder</a></li>
                                                    </ul>
                                                </li>                                    

                                            </ul>                                
                                        </div>
                                        <div class="data">
                                            Example of block with head, icon and buttons
                                        </div>
                                    </div>                          
                                </div>                     
                            </div>

                            <!-- modales -->
                            <?php foreach ($itemsPedido as $ipm): ?>                            
                                <div id="Modal_<?php echo $ipm['Item']['id']; ?>" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">                                                         
                                    <?php echo $this->Form->create('Pedidos', array('action' => 'editaparametros', 'id' => 'validate')); ?>
                                    <?php echo $this->Form->hidden('Item.id', array('value' => $ipm['Item']['id'])); ?>
                                    <?php echo $this->Form->hidden('Item.pedido_id', array('value' => $ipm['Item']['pedido_id'])); ?>
                                    <div class="modal-header">
                                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                                        <h3 id="myModalLabel">Edicion item del PEDIDO # <?php echo $ipm['Item']['pedido_id']; ?></h3>
                                    </div>        
                                    <div class="row-fluid">
                                        <div class="block-fluid">
                                            <div class="row-form">
                                                <div class="span12">
                                                    <span class="top title">URL:</span>
                                                    <input type="text" name="data[Item][url]" value="<?php echo $ipm['Item']['url']; ?>" class="validate[required]" />                        
                                                </div>
                                            </div>
                                            <div class="row-form">
                                                <div class="span3">
                                                    <span class="top title">Peso:</span>
                                                    <input type="number" name="data[Item][peso]" value="<?php echo $ipm['Item']['peso']; ?>" class="validate[required,custom[number]]" />
                                                </div>
                                            </div>
                                            <div class="row-form">
                                                <div class="span3">
                                                    <span class="top title">Costo:</span>
                                                    <input type="text" name="data[Item][costo]" value="<?php echo $ipm['Item']['costo']; ?>" class="validate[required,custom[number]]"/>
                                                </div>
                                            </div>                
                                            <div class="row-form">
                                                <div class="span3">
                                                    <span class="top title">Tax:</span>
                                                    <input type="text" name="data[Item][tax]" value="<?php echo $ipm['Item']['tax']; ?>" class="validate[required,custom[number]]"/>
                                                </div>
                                            </div>
                                            <div class="row-form">
                                                <div class="span3">
                                                    <span class="top title">Shipping:</span>
                                                    <input type="text" name="data[Item][shipping]" value="<?php echo $ipm['Item']['shipping']; ?>" class="validate[required,custom[number]]"/>
                                                </div>
                                            </div>
                                        </div>
                                    </div>                   
                                    <div class="modal-footer">
                                        <button type="submit" class="btn btn-primary"  aria-hidden="true">Guardar Cambios</button> 
                                        <button class="btn btn-warning" data-dismiss="modal" aria-hidden="true">Cerrar</button>            
                                    </div>
                                    </form>
                                </div>  
                            <?php endforeach; ?>
                            <!-- fin m -->

                            <div id="modalParametros" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">                                                         
                                <?php echo $this->Form->create('Pedidos', array('action' => 'editaitem', 'id' => 'validate')); ?>
                                <?php echo $this->Form->hidden('Item.id', array('value' => $ipm['Item']['id'])); ?>
                                <?php echo $this->Form->hidden('Item.pedido_id', array('value' => $ipm['Item']['pedido_id'])); ?>
                                <div class="modal-header">
                                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                                    <h3 id="myModalLabel">Edicion Parametros del PEDIDO # <?php echo $ipm['Item']['pedido_id']; ?></h3>
                                </div>        
                                <div class="row-fluid">
                                    <div class="block-fluid">
                                        <div class="row-form">
                                            <div class="span6">
                                                <span class="top title">Handy:</span>
                                                <input type="number" name="data[Item][peso]" value="<?php echo $detallePedido['Pedido']['handly']; ?>" class="validate[required,custom[number]]" />
                                            </div>
                                            
                                            <div class="span6">
                                                <span class="top title">Recepcion:</span>
                                                <input type="text" name="data[Item][costo]" value="<?php echo $ipm['Item']['costo']; ?>" class="validate[required,custom[number]]"/>
                                            </div>
                                        </div>
                                        <div class="row-form">
                                            <div class="span6">
                                                <span class="top title">Consolidacion:</span>
                                                <input type="number" name="data[Item][peso]" value="<?php echo $ipm['Item']['peso']; ?>" class="validate[required,custom[number]]" />
                                            </div>
                                            
                                            <div class="span6">
                                                <span class="top title">Flete:</span>
                                                <input type="text" name="data[Item][costo]" value="<?php echo $ipm['Item']['costo']; ?>" class="validate[required,custom[number]]"/>
                                            </div>
                                        </div>
                                        <div class="row-form">
                                            <div class="span6">
                                                <span class="top title">Desaduanizacion:</span>
                                                <input type="number" name="data[Item][peso]" value="<?php echo $ipm['Item']['peso']; ?>" class="validate[required,custom[number]]" />
                                            </div>
                                                                                        
                                        </div>
                                    </div>
                                </div>                   
                                <div class="modal-footer">
                                    <button type="submit" class="btn btn-primary"  aria-hidden="true">Guardar Cambios</button> 
                                    <button class="btn btn-warning" data-dismiss="modal" aria-hidden="true">Cerrar</button>            
                                </div>
                                </form>
                            </div>  

                        </div>                
                    </div> 

                </div>
            </div>                               

        </div>

    </div>

</div>